var version = '';
var updata = '-1';
function setAbout(version1,updata1){
	version = version1;
	updata = updata1;
	$('#version').html("Android V"+version1);
}
(function() {
	
//	$("body").on("click", ".menuClick", function (){
//		var id=$(this).attr("id");
//		if(id=="menu-home"){
//			window.location.href = window.rootPath + "/ui/mobileMain?device=mobile&identify=";
//		}else{
//			var url = $(this).attr('href');
//			window.location.href = url;
//		}
//			
//	});
//
//	$("body").on("click", "#goHome", function (){
//		window.location.href = window.rootPath + "/ui/mobileMain?device=mobile&identify=";
//	});
	
	$("body").on("click", ".contentClick", function (){
		if(personalType == 1){
			$(this).prop("href", "#rewardDetail");
		}else{
			$(this).prop("href", "#scoreDetail");
		}
	});

	$("body").on("click", ".settingsClick", function (){
		$(this).prop("href", "#settingsPage");
	});
	
	$("body").on("click", "#UserPic", function (){
		var userPic = $("#userPicUrl").val();
		if(userPic==undefined||userPic==""){
			userPic=defaultUserPic;
		}else{
			userPic=window.rootPath+'/'+userPic;
		}
		$("#settingsUserPic").attr("src",userPic);
		$(this).prop("href", "#userPicPage");
	});
	
	function getUserRewardDetail(data){
		var content ='';
		for (var i = 0; i < data.length; i++) {
			if(data[i].eventType){
				content += '<tr><td>'+data[i].eventType.eventName+'</td><td>'+moment(data[i].createTime).format('YYYY-MM-DD HH:mm')+'</td><td>'+data[i].register.registerEventAdress+'</td><td>'+data[i].rewardValue.toFixed(2)+'</td></tr>';
			}else{
				content += '<tr><td>'+data[i].details+'</td><td>'+moment(data[i].createTime).format('YYYY-MM-DD HH:mm')+'</td><td></td><td>-'+data[i].rewardValue.toFixed(2)+'</td></tr>';
			}
			
		}
		jQuery('#rewardTbody').append(content);
	}

	function getUserScoreDetail(data){
		var content ='';
		for (var i = 0; i < data.length; i++) {
			content += '<tr><td>'+data[i].registerEvent.registerEventType.eventName+'</td><td>'+moment(data[i].createTime).format('YYYY-MM-DD HH:mm')+'</td><td>'+data[i].registerEvent.registerEventAdress+'</td><td>'+data[i].scale.toFixed(2)+'</td></tr>';
		}
		jQuery('#scoreTbody').append(content);
	}
	function getUserScoreProcessDetail(data){
		var content ='';
		for (var i = 0; i < data.length; i++) {
			var process="";
			if(data[i].processType.title=="已分配"){
				process="接收超时";
			}else if(data[i].processType.title=="处理中"){
				process="处理超时";
			}
			content += '<tr><td>'+data[i].registerEvent.registerEventType.eventName+'</td><td>'
			+moment(data[i].registerEvent.createTime).format('YYYY-MM-DD HH:mm')+'</td><td>'
			+process+'</td><td>'
			+data[i].scale.toFixed(2)+'</td></tr>';
		}
		jQuery('#scoreProcessTbody').append(content);
	}
	
	var defaultUserPic = "../img/defaultUserPic.png";
	$("#userPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});
	
	$("#settingsUserPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});

	$("#UserPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});
	var personalType;
	$(document).ready(function() {
		personalType = jQuery('#personalType').val();
		if(personalType == ''){//默认游客界面
			personalType = "1";
		}
		//getData();
//		getNewData();
		var userPic = $("#userPicUrl").val();
		
		if(userPic==undefined||userPic==""){
			userPic=defaultUserPic;
		}else{
			userPic=window.rootPath+'/'+userPic;
		}
		$("#userPic").attr("src",userPic);
		$("#UserPic").attr("src",userPic);
		//$("#settingsUserPic").attr("src",userPic);
		
		getUserContent();
	});

	/**
	 * 初始化用户主体信息
	 */
	function getUserContent(){
		var url = window.rootPath + '/ui/Personal/getUser/'+personalType;
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				if(personalType == 1){	//游客 content
					setVisitorContent(data);
					jQuery('#rewardNumber').text("总分："+ data.contentNum);
					getUserRewardDetail(data.content.rewards);
				}else if(personalType == 2){	 //城管 content
					setManageContent(data);
					jQuery('#scoreNumber').text("本期扣分："+ (100-data.contentNum));
					getUserScoreDetail(data.content.scores);
					getUserScoreProcessDetail(data.content.scoresProcess);
				}else if(personalType == 3){  //领导 content
					setLeaderContent(data);
				}
				jQuery('#contentTitle').text(data.contentTitle);
				jQuery('#contentNum').text(data.contentNum);
			}
		})
	}
	/**
	 * 设置主体信息显示
	 */
	function setContentShow(visitor,manage,leader){
		if(visitor){
			jQuery('#visitor').show();
		}else{
			jQuery('#visitor').hide();
		}
		if(manage){
			jQuery('#manage').show();
		}else{
			jQuery('#manage').hide();
		}
		if(leader){
			jQuery('#leader').show();
		}else{
			jQuery('#leader').hide();
		}
		
	};
	
	/**
	 * 游客信息设置
	 */
	function setVisitorContent(data){
		jQuery('#visitorOneNum').text(data.oneNum);
		jQuery('#visitorOneTitle').text(data.oneTitle);
		jQuery('#visitorTwoNum').text(data.twoNum);
		jQuery('#visitorTwoTitle').text(data.twoTitle);
		jQuery('#visitorThreeNum').text(data.threeNum);
		jQuery('#visitorThreeTitle').text(data.threeTitle);
		jQuery('#visitorFourNum').text(data.fourNum);
		jQuery('#visitorFourTitle').text(data.fourTitle);
		
		setContentShow(true,false,false);
	}
	
	/**
	 * 城管信息设置
	 */
	function setManageContent(data){
		jQuery('#manageOneNum').text(data.oneNum);
		jQuery('#manageOneTitle').text(data.oneTitle);
		jQuery('#manageTwoNum').text(data.twoNum);
		jQuery('#manageTwoTitle').text(data.twoTitle);
		jQuery('#manageThreeNum').text(data.threeNum);
		jQuery('#manageThreeTitle').text(data.threeTitle);
		setContentShow(false,true,false);
	}
	
	/**
	 * 领导信息设置
	 */
	function setLeaderContent(data){
		jQuery('#oneNum').text(data.oneNum);
		jQuery('#oneTitle').text(data.oneTitle);
		jQuery('#twoNum').text(data.twoNum);
		jQuery('#twoTitle').text(data.twoTitle);
		jQuery('#threeNum').text(data.threeNum);
		jQuery('#threeTitle').text(data.threeTitle);
		setContentShow(false,false,true);
	}
	
	function buildVisitorListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].reportImgAdress;

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="visitorClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].reportAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].reportEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}

	function buildManageListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].registerEventImgAdress;
			
			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="manageClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].registerEventAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].registerEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].registerEventContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].registerEventAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}
	
	function buildLeaderListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].reportImgAdress;

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="leaderClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].reportAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].reportEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}
	
	function cleanReportDetail(){
		jQuery('#reportEventVariable').text('');
		jQuery('#reportEventType').text('');
		jQuery('#reportContent').text('');
		jQuery('#reportMemo').text('');
		var pic = rootPath + "/img/nopicture.png";
		jQuery('#avatart').attr("src",pic);
		jQuery('#locationDes').text('');
	}
	
	$("body").on("click", ".visitorClick", function (){
		var id = $(this).attr("defineId");
		cleanReportDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventReport/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventReport;
				jQuery('#reportEventVariable').text(data.reportEventVariable.title);
				jQuery('#reportEventType').text(data.reportEventType.eventName);
				jQuery('#reportContent').text(data.reportContent);
				jQuery('#reportMemo').text(data.reportMemo);
				var pic = data.reportImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#avatart').attr("src",pic);
				jQuery('#locationDes').text(data.reportAdress);
				
			}
		});
		$(this).prop("href", "#reportDetail");
	});

	function cleanRegisterDetail(){
		jQuery('#reportEventVariable').text('');
		jQuery('#reportEventType').text('');
		jQuery('#reportContent').text('');
		jQuery('#reportMemo').text('');
		var pic = rootPath + "/img/nopicture.png";
		jQuery('#avatart').attr("src",pic);
		jQuery('#locationDes').text('');
	}
	
	$("body").on("click", ".manageClick", function (){
		var id = $(this).attr("defineId");
		cleanRegisterDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventRegister/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventRegister;
				jQuery('#registerEventType').text(data.registerEventType.title);
				jQuery('#registerEventAccTime').text(moment(data.registerEventAccTime).format('YYYY-MM-DD HH:mm'));
				jQuery('#registerEventContent').text(data.registerEventContent);
				jQuery('#registerEventMemo').text(data.registerEventMemo);
				var pic = data.registerEventImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#registerAvatart').attr("src",pic);
				jQuery('#locationDesRegister').text(data.registerEventAdress);
				
			}
		});
		$(this).prop("href", "#registerDetail");
	});

	$("body").on("click", "#aboutApp", function (){
		
		if(_phoneType=="Android"){
			window.jump.getAbout(); 
		}else if(_phoneType=="Ios"){
			
		}else{
			
		}
		
		$.mobile.changePage('#aboutPage');
	});

	$("body").on("click", "#application", function (){
		$.mobile.changePage('#applicationPage');
	});

	$("body").on("click", "#social", function (){
		$.mobile.changePage('#socialPage');
	});
	
	$("body").on("click", ".leaderClick", function (){
		var id = $(this).attr("defineId");
		cleanReportDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventReport/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventReport;
				jQuery('#reportEventVariable').text(data.reportEventVariable.title);
				jQuery('#reportEventType').text(data.reportEventType.eventName);
				jQuery('#reportContent').text(data.reportContent);
				jQuery('#reportMemo').text(data.reportMemo);
				var pic = data.reportImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#avatart').attr("src",pic);
				jQuery('#locationDes').text(data.reportAdress);
				
			}
		});
		$(this).prop("href", "#reportDetail");
	});
	
	$("body").on("click", ".manageNumDiv", function (){
		window.location.href = window.rootPath + "/ui/archive?device=mobile&identify=&type="+$(this).index(".manageNumDiv");
	})

	$("body").on("click", ".visitorNumDiv", function (){
		window.location.href = window.rootPath + "/ui/visitorTask?device=mobile&identify=&type="+$(this).index(".visitorNumDiv");
	})
	
	
	$("#file_upload").change(function () {
		var $file = $(this);
		var fileObj = $file[0];
		var windowURL = window.URL || window.webkitURL;
		var dataURL;
		var $img = $("#settingsUserPic");
	    
		if (fileObj && fileObj.files && fileObj.files[0]) {
			dataURL = windowURL.createObjectURL(fileObj.files[0]);
			$img.attr('src', dataURL);
			
		} else {
			dataURL = $file.val();
			var imgObj = document.getElementById("preview");
			// 两个坑:
			// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
			// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
			imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
			
		}
	});
	
	jQuery('#settingsUserPic').on('click', function () {
		return  $("#file_upload").click();
	});
	
	jQuery('#saveBtn').on('click', function () {
		var data='';
		var originPassword=$("#originPassword").val();//原密码
		
		var alterPassword=$("#alterPassword").val();
		var confirmPassword=$("#confirmPassword").val();
		if(originPassword!=null&&originPassword!=''){//修改密码
			var oldPassword=$("#oldPassword").val();
			originPassword=sha256_digest($("#originPassword").val());
			if(oldPassword!=originPassword){
				alert("原密码输入错误");
				return false;
			}else{
				if(alterPassword==''&&alterPassword==null){
					alert("新密码不能为空");
					return false;
				}else{
					if(alterPassword!=confirmPassword){
						alert("两次输入密码不相同！");
						return false;
					}else{
						data={"password":alterPassword};
					}
				}
			}
		}else{
			data='';//只修改用户头像
			
		}
		$.ajaxFileUpload({
			url : window.rootPath + "/ui/Personal/saveUserPic/"+jQuery('#userId').val()+"?password="+alterPassword,
			secureuri : false,// 是否需要安全协议
			fileElementId : "file_upload",// 文件上传的Id属性
			dataType : 'json',
			data : '',
			success : function(result) {
				$("#alterPassword").val('');
				$("#originPassword").val('');
				$("#confirmPassword").val('');
				var userPic = result.userPic;
				$("#userPicUrl").val(userPic);
				if(userPic==undefined||userPic==""){
					userPic=defaultUserPic;
				}else{
					userPic=window.rootPath+'/'+userPic;
				}
				$("#userPic").attr("src",userPic);
				$("#UserPic").attr("src",userPic);
				//修改了密码应当重新登录
				if(alterPassword==''){
					$('#goSettings').click();
				}else{
					location.href=rootPath+"/ui/login?device=mobile&identify="
				}
				
				//window.location.reload();
			}
		});
	});
	
})();